﻿var baseURL = '';
var baseAPIURL = baseURL + 'api/__/';

function isEmpty(v) {
    return v == null || v == '';
}

function id(v, dom) {
    dom = dom || this.document;
    return dom.getElementById(v);
}
 
Date.prototype.getLocalDate = function() {
    return  new Date( this.getTime() + 8 * 60 * 60 * 1000   ).toISOString()
}
function logout() {
    $.post(
        baseURL + 'logout', 
        function(data){
            checkAuth(data);
            if(data.RET_CODE == '00') {
                top.location.href = 'login.html';
            }
        }
    )
    .error(function(){ top.location.href = 'login.html'; }); 
}

(function () {

    //menu switch
    $(function () {
        $('.menu').delegate('.item', 'click', function () {
            $(this).addClass("active").siblings().removeClass("active");
        });
    });

})();

//表格控件注册
(function () {
    window.registerDatGrid = function (tableID, options) {
        $.ajaxSetup ({
            cache: false //关闭AJAX相应的缓存
        });

        options = options || {};
        var container = $(tableID ? "#" + tableID : ".dataTableList > table");
        if (container.length) {
            options.onLoadSuccess = function(){
                // iframeHeight($(parent.document).find('#rightContent')[0]);
                return options.onLoadSuccess || function(){};
            };
            container.datagrid(
                $.extend({
                    pagination: true,
                    pageList: [10, 30, 50],
                    pageSize: 10,
                    rownumbers: false,
                    fitColumns: true,
                }, options));

            var pager = container.datagrid('getPager');
            $(pager).pagination({
                onSelectPage: options.onSelectPage,
                beforePageText: '第',
                afterPageText: '页&nbsp;共&nbsp;{pages}&nbsp;页',
                displayMsg: '当前显示{from} 到 {to}&nbsp;条记录&nbsp;共&nbsp;{total}&nbsp;条记录'

            });
            return pager;
        }
    };
    
})();

function iframeHeight(iframe, initHeight) {
    /*if($(".redoPage").length > 0 ){
        initHeight = 1100
    }*/
    var id = "rightContent";
    initHeight = initHeight || 500;
    if (!!iframe) id = iframe.id;
    var ifm = document.getElementById(id);
    if (!ifm) ifm = parent.document.getElementById(id);
    var main = document.getElementsByClassName("main")[0];
    if(!main) main = parent.document.getElementsByClassName("main")[0];
    ifm.height = initHeight;
    console.log(ifm);
    var height = ifm.contentWindow.document.body.offsetHeight;
    if($(ifm.contentWindow.document).find(".redoPage").length > 0){
        height += 365
    }
    ifm.height = Math.max(height, initHeight) + 20;
    main.style.height = Math.max(Math.max(height, initHeight) + 90, top.document.body.clientHeight ) + 'px';
};

    // window.setInterval("reinitIframe()", 200);

//param = "APP_ID"
function getUrlParameters(param) { 
    if(isEmpty(param)) {
        return null;
    }    
    var query = window.location.search; //获取URL地址中？后的所有字符  
    var iLen = param.length;            //获取参数名称长度  
    var iStart = query.indexOf(param);  //获取该参数名称的真实索引  
    if (iStart == -1)//-1为没有该参数  
        return "";  
    iStart += iLen + 1;  
    var iEnd = query.indexOf("&", iStart);  //获取第二个参数的真实索引  
    if (iEnd == -1)//只有一个参数  
        return query.substring(iStart);     //获取单个参数的参数值  
    return query.substring(iStart, iEnd);   //获取第二个参数的值  
}  


function checkAuth(data) {
    data = typeof data == 'string' ? eval('(' + data + ')') : data;
    if(data && data.RET_CODE == '30') {
        top.location.href = 'login.html';
        return false;
    }
    return true;
}


function includes(arr, ele) {
    if (!arr || arr.length==0) {
        return false;
    }
    else if(!ele || ele.length==0) {
        return false;
    }
    else {
        for(var i=0; i<arr.length; i++) {
            if(ele == arr[i]) {
                return true;
            }
        }
        return false;
    }
}

/*获取文件路径名*/
function getFileName(filepath) {
    if(filepath) {
        if(filepath.length == 1) {
            return filepath;
        }
        else {
            var paths = filepath.split('\\');
            return paths[paths.length-1];   
        }        
    }
    else
        return null;
}

/*文件上传失败*/
function uploadErrorHandler(data, status, e) {
    $("#tip_info").html("");
    $("#loading_info",window.parent.document).css({display: "none"});
    // alert('上传失败');
    $("#failMsg",window.parent.document).html("文件上传失败!");
    $("#upload_fail",window.parent.document).css({display: "block"});
    $("#tip_info").html("未选择文件");
}


/*生成表格页脚*/
function renderOrgPrudTableFooter(currentPage, pageCount,countPiece) {
    $(".tablePageComp ul").html('<li class="firstPage icons" role="firstPage"></li><li class="upPage icons" role="upPage"></li><li class="downPage icons" role="downPage"></li><li class="lastPage icons" role="lastPage"></li>');
    currentPage = currentPage || 1;
    pageCount = pageCount || 1;
    countPiece = countPiece || 0;
    var pageNo, //显示的页数
        pageNoInner, //插入的内容
        insertEle; //插入的元素
    $(".countPage").text(pageCount);
    $(".countPiece").text(countPiece)
    if (pageCount > 5) {


        if (currentPage > 2 && currentPage < pageCount - 2) {
            for (i = 0; i < 5; i++) {
                pageNo = currentPage - 2 + i;
                pageNoInner = "<span>" + pageNo + "</span>";
                insertEle = $(document.createElement("li")).attr("role", "pageOption").html(pageNoInner);
                i == 2 ? insertEle.addClass("currentPage") : false;
                insertEle.insertBefore($(".downPage"));
            }
        } else if (currentPage < 3) {
            for (var i = 0; i < 5; i++) {
                pageNo = i + 1;
                pageNoInner = "<span>" + pageNo + "</span>";
                insertEle = $(document.createElement("li")).attr("role", "pageOption").html(pageNoInner);
                i + 1 == currentPage ? insertEle.addClass("currentPage") : false;
                insertEle.insertBefore($(".downPage"));
            }
        } else {
            for (i = 0; i < 5; i++) {
                pageNo = pageCount - 4 + i;
                pageNoInner = "<span>" + pageNo + "</span>";
                insertEle = $(document.createElement("li")).attr("role", "pageOption").html(pageNoInner);
                i == 4 + currentPage - pageCount ? insertEle.addClass("currentPage") : false;
                insertEle.insertBefore($(".downPage"));
            }
        }
    } else {
        for (var i = 1; i < pageCount + 1; i++) {
            pageNo = i;
            pageNoInner = "<span>" + pageNo + "</span>";
            insertEle = $(document.createElement("li")).attr("role", "pageOption").html(pageNoInner);
            i == currentPage ? insertEle.addClass("currentPage") : false;
            insertEle.insertBefore($(".downPage"));
        }
    }
}


function searchProxyfun() {
    var _self = $(this), //触发该函数的元素
        clickElementRole = _self.attr("role"), //触发该函数的元素角色
        currentPage = ($(".currentPage").text()) * 1, //当前页
        countPage = ($(".countPage").text()) * 1, //总页数
        pageNo,
        clickCase = {
            firstPage: 1,
            upPage: (currentPage - 1) > 0 ? currentPage - 1 : 1,
            downPage: (currentPage + 1) < countPage ? (currentPage + 1) : countPage,
            lastPage: countPage,
            pageOption: _self.text()
        };
    pageNo = clickCase[clickElementRole];
    searchOrgDict(pageNo);
}

function searchOrgDict(pageNo) {
    pageNo = pageNo || 1;
    var queryDistParams = {};
    $('[searchOption]').map(function() {
        if ($(this).val().trim() != "") {
            queryDistParams[$(this).attr("searchOption")] = $(this).val().trim()
        }
    });
    $('[role="datagrid"]').datagrid('load', {
        "pageNum": pageNo,
        "pageSize": 10,
        "query": queryDistParams,
        "sort": "UPDATE_DATE",
        "order": "desc"
    });
    return queryDistParams;
}

/*隐藏弹出框*/
function hideVilidate(_self){
    if($(_self).hasClass("v-panel")){
        $(_self).parent().css({display: "none"});
        $('body').css({overflow:"auto"});   
    }else{
        _self = $(_self).parent()[0];
        hideVilidate(_self);
    }
}

/*展示提示消息*/
function showPromptInfo(promptEle,insertEle,promptMsg){
    $(insertEle, window.parent.document).html(promptMsg);
    $(promptEle, window.parent.document).css({
        display: "block"
    });
}

function timeC(d){
    var issd = this.id == 'd_start';
    timeRules();
    var sd = new Date($('#d_start').datebox('getValue'))
    var ed = new Date($('#d_end').datebox('getValue')) ;
    var d1s = new Date(ed.getFullYear(), ed.getMonth(), ed.getDate()+29);
    if(issd){
        if(sd.getTime() < d1s.getTime()){
            $('#d_end').datebox('setValue', '').datebox('showPanel');
        }
    }
    if($('#d_end').datebox('getValue') != ""){
        if(ed.getTime() < sd.getTime()){
            $('#d_end').datebox('setValue', '').datebox('showPanel');
        }
    }
}

function timeRules(){
    $('#d_end').datebox('calendar').calendar({
        validator: function (date) {
            var now1 = new Date($('#d_start').datebox('getValue'));
            var d2s = new Date(now1.getFullYear(), now1.getMonth(), now1.getDate());
            var d1s = new Date(now1.getFullYear(), now1.getMonth(), now1.getDate()+29);
            var now = new Date();
            var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
            return date <= d1s && date <= d1 && date >= d2s;
        }
    });
}